﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json.Converters;
using System.Collections;
using Newtonsoft.Json;
using System.Runtime.InteropServices;
using System.Text;

/// <summary>
///SoapHolder 的摘要说明
/// </summary>
public class ServiceTransfer
{

    public string ReturnValue { get; set; }
    public string Exception { get; set; }
    public IList<String> ListValue { get; set; }

    private ServiceTransfer() { }

    internal static IsoDateTimeConverter timeConverter = null;

    static ServiceTransfer()
    {
        timeConverter = new IsoDateTimeConverter();
        timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
    }

    public static ServiceTransfer FromListValue(IEnumerable list)
    {
        string temp;
        ServiceTransfer holder = new ServiceTransfer();
        holder.ListValue = new List<String>();
        foreach (var item in list)
        {
            temp = JsonConvert.SerializeObject(item, Formatting.Indented, timeConverter);
            temp = Convert.ToBase64String(Encoding.UTF8.GetBytes(temp));
            holder.ListValue.Add(temp);
        }
        return holder;
    }
    public static ServiceTransfer FromReturnValue(Object obj)
    {
        ServiceTransfer holder = new ServiceTransfer();
        holder.ReturnValue = JsonConvert.SerializeObject(obj, Formatting.Indented, timeConverter);
        holder.ReturnValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(holder.ReturnValue));
        return holder;
    }
    public static ServiceTransfer FromException(_Exception ex)
    {
        ServiceTransfer holder = new ServiceTransfer();
        holder.Exception = Convert.ToBase64String(Encoding.UTF8.GetBytes(ex.Message));
        return holder;
    }
}